import time

from fastapi import FastAPI
import asyncio

app = FastAPI()


@app.get("/")
async def root():
    tasks = []

    async def perform_task(task_id):
        print("__")
        # 这里可以编写你的并发请求逻辑
        await asyncio.sleep(5)
        # time.sleep(5)
        print(task_id)
        return f"Task {task_id} completed."

    for i in range(10):
        task = asyncio.create_task(perform_task(i))
        tasks.append(task)
    print("全部加载进去了")


    results = await asyncio.gather(*tasks)
    return results


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="127.0.0.1", port=8000)